#pragma once

#include <boost/graph/adjacency_list.hpp>

namespace boost {

  template <class Config>
  std::ostream&
  operator<<( std::ostream& out, 
              directed_graph_helper<Config>& g_)
  {
    typedef typename Config::graph_type Graph;
    typedef typename Config::vertex_iterator vertex_iterator;
    typedef typename Config::out_edge_iterator out_edge_iterator;
    typedef typename Config::adjacency_iterator adjacency_iterator;
    typedef typename Config::edge_iterator edge_iterator;

    Graph& g =static_cast<Graph&>(g_);
    
    typename property_map<Graph,vertex_index_t>::type index =get(vertex_index,g);
    
    //Graph& g =const_cast<Graph&>(cg);

    out <<num_vertices(g) <<" vertices ,"
        <<num_edges(g) <<" edges" 
        <<std::endl;
    
    vertex_iterator viter, vlast;
    tie( viter, vlast) =vertices( g);
    for( ; viter !=vlast; ++viter){
      out <<index[*viter] <<", out:("
          <<out_degree( *viter, g) <<")[";
      out_edge_iterator eiter, elast;
      tie( eiter, elast) =out_edges( *viter, g);
      for( ; eiter !=elast; ++eiter){
        out <<index[target( *eiter, g)] <<",";
      }
      out <<"]";

      out <<", adj:[";
      adjacency_iterator adj_iter, adj_last;
      tie( adj_iter, adj_last) =adjacent_vertices( *viter, g);
      for( ; adj_iter !=adj_last; ++adj_iter)
        out <<index[*adj_iter] <<",";
      out <<"]";
      
      out <<std::endl;
    }

    out <<"edges:(" <<num_edges( g) <<")" <<std::endl;
    edge_iterator edge_iter, edge_last;
    tie( edge_iter, edge_last) =edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<"(" <<index[source(*edge_iter,g)]
          <<"," <<index[target(*edge_iter,g)]
          <<"),";
    }
    out<<std::endl;

    return out;
  }

  template <class Config>
  std::ostream&
  operator<<( std::ostream& out, 
              undirected_graph_helper<Config>& g_)
  {
    typedef typename Config::graph_type        Graph;
    typedef typename Config::vertex_iterator   vertex_iterator;
    typedef typename Config::out_edge_iterator out_edge_iterator;
    typedef typename Config::in_edge_iterator  in_edge_iterator;
    typedef typename Config::adjacency_iterator adjacency_iterator;
    typedef typename Config::edge_iterator edge_iterator;
    
    Graph& g =static_cast<Graph&>(g_);
    
    typename property_map<Graph,vertex_index_t>::type index =get(vertex_index,g);

    out <<num_vertices(g) <<" vertices ,"
        <<num_edges(g) <<" edges" 
        <<std::endl;
    
    vertex_iterator viter, vlast;
    tie( viter, vlast) =vertices( g);
    for( ; viter !=vlast; ++viter){
      
      out <<index[*viter] <<", out:(" 
          <<out_degree( *viter, g) <<")[";
      out_edge_iterator out_iter, out_last;
      tie( out_iter, out_last) =out_edges( *viter, g);
      for( ; out_iter !=out_last; ++out_iter){
        out <<index[target( *out_iter, g)] <<",";
      }
      out <<"]";
      
      out <<", in:(" 
          <<in_degree( *viter, g) <<")[";
      in_edge_iterator in_iter, in_last;
      tie( in_iter, in_last) =in_edges( *viter, g);
      for( ; in_iter !=in_last; ++in_iter){
        out <<index[source( *in_iter, g)] <<",";
      }
      out <<"]";

      out <<", adj:[";
      adjacency_iterator adj_iter, adj_last;
      tie( adj_iter, adj_last) =adjacent_vertices( *viter, g);
      for( ; adj_iter !=adj_last; ++adj_iter)
        out <<index[*adj_iter] <<",";
      out <<"]";
      
      out <<std::endl;
    }
    
    out <<"edges:(" <<num_edges( g) <<")" <<std::endl;
    edge_iterator edge_iter, edge_last;
    tie( edge_iter, edge_last) =edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<"(" <<index[source( *edge_iter, g)]
          <<"," <<index[target( *edge_iter, g)]
          <<"),";
    }

    out<<std::endl;
    
    return out;
  }

  template <class Config>
  std::ostream&
  operator<<( std::ostream& out, 
              bidirectional_graph_helper<Config>& g_)
  {
    typedef typename Config::graph_type        Graph;
    typedef typename Config::vertex_iterator   vertex_iterator;
    typedef typename Config::out_edge_iterator out_edge_iterator;
    typedef typename Config::in_edge_iterator  in_edge_iterator;
    typedef typename Config::adjacency_iterator adjacency_iterator;
    typedef typename Config::edge_iterator      edge_iterator;

    Graph& g =static_cast<Graph&>(g_);

    typename property_map<Graph,vertex_index_t>::type index =get(vertex_index,g);

    vertex_iterator viter, vlast;
    tie( viter, vlast) =vertices( g);
    for( ; viter !=vlast; ++viter){
      
      out <<index[*viter] <<", out:("
          <<out_degree( *viter, g) <<")[";
      out_edge_iterator out_iter, out_last;
      tie( out_iter, out_last) =out_edges( *viter, g);
      for( ; out_iter !=out_last; ++out_iter){
        out <<index[target( *out_iter, g)] <<",";
      }
      out <<"]";
      
      out <<", in:(" 
	  <<in_degree( *viter, g) <<")[";
      in_edge_iterator in_iter, in_last;
      tie( in_iter, in_last) =in_edges( *viter, g);
      for( ; in_iter !=in_last; ++in_iter){
        out <<index[source( *in_iter, g)] <<",";
      }
      out <<"]";
      
      out <<", adj:[";
      adjacency_iterator adj_iter, adj_last;
      tie( adj_iter, adj_last) =adjacent_vertices( *viter, g);
      for( ; adj_iter !=adj_last; ++adj_iter)
        out <<index[*adj_iter] <<",";
      out <<"]";

      out<<std::endl;
    }

    out <<"edges:(" <<num_edges( g) <<")" <<std::endl;
    edge_iterator edge_iter, edge_last;
    tie( edge_iter, edge_last) =edges( g);
    for( ; edge_iter !=edge_last; ++edge_iter){
      out <<"(" <<index[source( *edge_iter, g)]
          <<"," <<index[target( *edge_iter, g)]
          <<"),";
    }
    out <<std::endl;
    
    return out;
  }
}
